package com.itboye.jigongbao.adpapter

import android.content.Context
import com.itboye.jigongbao.R
import com.itboye.jigongbao.activity.JiGongBillsActivity
import com.itboye.jigongbao.adpapter.baseAdapter.MutiplyCommonAdapter
import com.itboye.jigongbao.adpapter.baseAdapter.ViewHolder
import com.itboye.jigongbao.bean.JiYiBiBean
import com.itboye.jigongbao.utils.ByTimeUtils.timeFormat
import org.json.JSONObject

/**
 * Created by Administrator on 2017/11/29.
 */
class JiGongBillsListAdapter(private val context: Context, datas: List<JiYiBiBean.JiYiBiEntity>, vararg layoutId: Int) : MutiplyCommonAdapter<JiYiBiBean.JiYiBiEntity>(context, datas, layoutId[0]) {

    var activity: JiGongBillsActivity? = null
    override fun getItemViewType(position: Int): Int {
        return 0
    }

    override fun getItemPosition(position: Int) {
    }

    fun setAdapterActivity(activity: JiGongBillsActivity) {
        this.activity = activity
    }

    override fun convert(holder: ViewHolder?, t: JiYiBiBean.JiYiBiEntity?, type: Int, position: Int) {
        holder!!.setText(R.id.txt_date, "${timeFormat("yyyyMMdd", "dd", t!!.daytime)}日")
        holder!!.setText(R.id.txt_name, t!!.worker_name)
        var extra: JSONObject? = null
        try {
            extra = JSONObject(t!!.extra)
        } catch (e: Exception) {

        }

        when (t!!.item_type_key) {
            "00S002003" -> {
                var gong = 0
                //点工
                if (extra!!.has("normal_worktime")) {
                    gong += extra.getString("normal_worktime").toInt() / 8
                }
                if (extra.has("over_worktime")) {
                    gong += extra.getString("over_worktime").toInt() / 6
                }
                holder!!.setText(R.id.txt_hours, "（${gong}个工）点工")
            }
            "00S002002" -> {
                //包工
                //点工
                var gong = 0
                if (extra!!.has("normal_worktime")) {
                    gong += extra.getString("normal_worktime").toInt() / 8
                }
                if (extra.has("over_worktime")) {
                    gong += extra.getString("over_worktime").toInt() / 6
                }
                holder!!.setText(R.id.txt_hours, "（${gong}个工）包工")
            }
            "00S002004" -> {
                //借支
                holder!!.setText(R.id.txt_hours, "借支")
            }
            "00S002005" -> {
                //结算
                holder!!.setText(R.id.txt_hours, "结算")
            }
            else -> {
            }
        }
        if (t!!.money >= 0) {
            holder!!.setTextColor(R.id.txt_money, context.resources.getColor(R.color.gallery_green))
            holder!!.setText(R.id.txt_money, "+￥${t!!.money / 100}")
        } else {
            holder!!.setTextColor(R.id.txt_money, context.resources.getColor(R.color.gallery_red))
            holder!!.setText(R.id.txt_money, "-￥${t!!.money / 100}")
        }
//        holder!!.setOnClickListener(R.id.txt_mingxi, position, activity)
    }
}